package movierecomending;

import java.io.File;
import java.util.HashMap;
import java.util.Vector;

import rtv.api.matrix.Element;
import rtv.api.matrix.LSAMatrix;
import compositionality.core.DenseWordVector;
import compositionality.inferrers.scoreComposers.Sum;
import data.CandidateScore;

public class MatrixInterface {

	
	static LSAMatrix wordMatrix;
	static LSAMatrix documentMatrix;

	
	public MatrixInterface(String path) throws Exception {
		loadMatrix(path);
	}


	/**
	 * @param args
	 * @throws Exception 
	 *//*
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		String[] keywords={"amore"};
		
		MatrixInterface matrix=new MatrixInterface(args[0]);
		
		
		matrix.getSimilarMovie("Amazons_#resource",null);
		matrix.getSimilarKeyword(keywords, null);
		
	}*/
	
	
	
	
	public static void loadMatrix(String path) throws Exception{
		
		wordMatrix = new LSAMatrix(path+File.separator+"LSAWord.txt");
		documentMatrix = new LSAMatrix(path+File.separator+"LSADoc.txt");
	}
	
	
	public HashMap<String,CandidateScore> getSimilarMovie(String film,HashMap<String,CandidateScore> candidates){
			
			
		CandidateScore c = null;
		float[] inputVec = documentMatrix.getCoordinates(film);
		if (inputVec != null) {
			
			for (String s:candidates.keySet()){
				c=candidates.get(s);
				c.setTitlescore(documentMatrix.getSim(s, inputVec));
				candidates.put(s,c);
			}
		}
			return candidates;
	}

	public HashMap<String, CandidateScore> getSimilarKeyword(String[] keywords,
			HashMap<String, CandidateScore> candidates) {
			
			CandidateScore c = null;

			boolean denseVectorSetted = false;
			
			HashMap<String,float[]> t=new HashMap<String,float[]>();
			float[] subVec = null;
			
			float[] inputVec = null; 
			
			DenseWordVector wv = new DenseWordVector(inputVec);
			
			for (String key:keywords){				
				System.out.println("Get the vector for "+key.trim());
				subVec = wordMatrix.getCoordinates(key.trim());    //da lemmatizzare!!!
					if (subVec != null) {
					if (!denseVectorSetted) {
						inputVec = new float[subVec.length];
							for (int j = 0; j < inputVec.length; j++) {
								inputVec[j] = (float) 0.0;
							}
							wv.setDenseWordVector(inputVec);
							denseVectorSetted = true;
				}	
				DenseWordVector temp = new DenseWordVector(subVec);
				Sum scorecomposer = new Sum();
				wv = temp.getComposedWordVector(scorecomposer, wv);
			}								
		}
				
		for (String s:candidates.keySet()){
			
			c=candidates.get(s);
			c.setKeywordscore(documentMatrix.getSim(s, (wv.getDenseVectorArray())));
			candidates.put(s,c);
		}
		return candidates;
	}
}
